package com.market.domain.rebate.service;

import com.market.domain.rebate.event.SendRebateMessageEvent;
import com.market.domain.rebate.model.aggregate.BehaviorRebateAggregate;
import com.market.domain.rebate.model.entity.BehaviorEntity;
import com.market.domain.rebate.model.entity.BehaviorRebateOrderEntity;
import com.market.domain.rebate.model.entity.TaskEntity;
import com.market.domain.rebate.model.valobj.DailyBehaviorRebateVO;
import com.market.domain.rebate.model.valobj.TaskStateVO;
import com.market.domain.rebate.reposiory.IBehaviorRebateRepository;
import com.market.types.common.Constants;
import com.market.types.event.BaseEvent;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

@Service
public class BehaviorRebateService implements IBehaviorRebateService {

    @Resource
    IBehaviorRebateRepository behaviorRebateRepository;
    @Resource
    private SendRebateMessageEvent sendRebateMessageEvent;

    @Override
    public List<String> createOrder(BehaviorEntity behaviorEntity) {

        //1.查询返利活动配置
        //根据behaviorTypeVO查询相应行为的返利策略
        List<DailyBehaviorRebateVO> dailyBehaviorRebateVOList = behaviorRebateRepository.queryDailyBehaviorRebateConfig(behaviorEntity.getBehaviorTypeVO());
        if(dailyBehaviorRebateVOList== null || dailyBehaviorRebateVOList.isEmpty())
                return new ArrayList<>();

        //2.组装聚合对象
        List<String> orderIdList = new ArrayList<>();
        List<BehaviorRebateAggregate> behaviorRebateAggregateList = new ArrayList<>();
        for(DailyBehaviorRebateVO dailyBehaviorRebateVO : dailyBehaviorRebateVOList) {
            // 拼装业务ID；用户ID_返利类型_外部透彻业务ID
            String bizId = behaviorEntity.getUserId() + Constants.UNDERLINE + dailyBehaviorRebateVO.getRebateType() + Constants.UNDERLINE + behaviorEntity.getOutBusinessNo();
            BehaviorRebateOrderEntity behaviorRebateOrderEntity = BehaviorRebateOrderEntity.builder()
                        .userId(behaviorEntity.getUserId())
                        .orderId(RandomStringUtils.randomNumeric(12))
                        .behaviorType(dailyBehaviorRebateVO.getBehaviorType())
                        .rebateDesc(dailyBehaviorRebateVO.getRebateDesc())
                        .rebateType(dailyBehaviorRebateVO.getRebateType())
                        .rebateConfig(dailyBehaviorRebateVO.getRebateConfig())
                        .outBusinessNo(behaviorEntity.getOutBusinessNo())
                        .bizId(bizId)
                        .build();
            orderIdList.add(behaviorRebateOrderEntity.getOrderId());

            // MQ 消息对象
            SendRebateMessageEvent.RebateMessage rebateMessage = SendRebateMessageEvent.RebateMessage.builder()
                    .userId(behaviorEntity.getUserId())
                    .rebateType(dailyBehaviorRebateVO.getRebateType())
                    .rebateConfig(dailyBehaviorRebateVO.getRebateConfig())
                    .bizId(bizId)
                    .build();
            // 构建事件消息
            BaseEvent.EventMessage<SendRebateMessageEvent.RebateMessage> rebateMessageEventMessage = sendRebateMessageEvent.buildEventMessage(rebateMessage);

            //组装任务对象Task
            TaskEntity taskEntity = TaskEntity.builder()
                    .userId(behaviorEntity.getUserId())
                    .topic(sendRebateMessageEvent.topic())
                    .messageId(rebateMessageEventMessage.getId())
                    .message(rebateMessageEventMessage)
                    .state(TaskStateVO.create)
                    .build();

            //组装聚合对象
            BehaviorRebateAggregate behaviorRebateAggregate = BehaviorRebateAggregate.builder()
                    .userId(behaviorEntity.getUserId())
                    .behaviorRebateOrderEntity(behaviorRebateOrderEntity)
                    .taskEntity(taskEntity)
                    .build();
            behaviorRebateAggregateList.add(behaviorRebateAggregate);

        }

        //3.存储聚合对象数据
        //再传入userid原因：记录是分库分表的，先拿到id仓储层就不需要再去对象里面拆解获取
        behaviorRebateRepository.saveUserRebateRecord(behaviorEntity.getUserId() ,behaviorRebateAggregateList);
        //4.返回订单id集合
        return orderIdList;
    }

    @Override
    public List<BehaviorRebateOrderEntity> queryOrderByOutBusinessNo(String userId, String outBusinessNo) {
        return behaviorRebateRepository.queryOrderByOutBusinessNo(userId, outBusinessNo);
    }
}
